package cn.b.sky.user.freemarker;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.i18n.enums.LocaleResourceEnum;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import freemarker.core.Environment;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
 * 
 * <strong>Title : LabelDirective.java<br>
 * </strong> <strong>Package : cn.b.sky.user.taglib<br>
 * </strong> <strong>Description : </strong>自定义国际化label标签<br>
 * <strong>Create on : 2015-8-18 上午10:15:17<br>
 * </strong>
 * <p>
 * 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
public class LabelDirective implements TemplateDirectiveModel {

  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    // 检查参数是否传入
    if (params.isEmpty()) {
      throw new TemplateModelException("This directive have no parameters.");
    }

    if (loopVars.length != 0) {
      throw new TemplateModelException("This directive doesn't allow loop variables.");
    }
    // 真正开始处理输出内容  
    Writer out = env.getOut();
    //考虑到默认值，防止特别依赖数据库
    if (body != null) {
      String value = getI18nLabelContent(params);
      if (!StringUtils.isBlank(value)) {
        out.write(value);
      } else {
        body.render(out);
      }
    } else {
      out.write(getI18nLabelContent(params));
    }
  }

  private String getI18nLabelContent(Map<?, ?> params) {

    String language = SecurityContextUtil.getLanguageCode();
    String siteCode = SecurityContextUtil.getSiteCode();
    LocaleResourceBundle bundle = LocaleResourceBundle.getBundle(siteCode,
        LocaleResourceEnum.COMMONRESOURCES.getValue(), language);
    //未开启国际化，或者没有相关的国际化资源
    if (bundle == null || bundle.getI18nSet() != 0) {
      return "";
    }
    StringBuilder sb = new StringBuilder("");
    String keystr = params.get("key").toString();
    if (!StringUtils.isBlank(keystr)) {
      String[] keys = keystr.split(SystemConstant.LABLE_SPLIT);
      //支持单个key，然后加上参数
      if (keys.length == 1) {
        String parameter = null;
        if (params.containsKey("parameter")) {
          parameter = (String) params.get("parameter");
        }
        sb.append(LocaleResourceBundle.getCommonResourceLabel(siteCode, language, keys[0],
            parameter == null ? null : parameter.split(",")));
      } else {
        for (String key : keys) {
          String content = LocaleResourceBundle.getCommonResourceLabel(siteCode, language, key);
          if (!StringUtils.isBlank(content)) {
            sb.append(content);
            //非中文的话单词之间加空格
            if (!language.equals(SystemConstant.LANGUAGE_CODE_ZH)) {
              sb.append(" ");
            }
          }
        }
      }
    }
    return sb.toString();
  }

}
